package com.example.demon.handler;

import com.example.demon.exception.BaseException;
import com.example.demon.result.CodeMsg;
import com.example.demon.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = {BaseException.class,Exception.class})
    public Result<String> errorHandler(Exception ex){

        if (ex instanceof BaseException){
            return Result.error(((BaseException) ex).getCodeMsg());
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }

}
